<?php
namespace Common\Controller;
use Think\Controller;
use Common\Controller\BaseController;

class WapController extends BaseController {

    protected function _initialize()
    {
        parent::_initialize();
        return true;

        //本地测试环境下，模拟微信授权数据写入session中；
        if(1 || strpos($_SERVER['HTTP_HOST'], "127.0.0.1")!==false
                || strpos($_SERVER['HTTP_HOST'], "localhost")!==false){
            session("token", "figol");
            $user = array(
                "subscribe" => 1,
                "openid" => "oirs41rRN7S74YMwzTCV5-ewXS6c",
                "nickname" => "高楼平地起",
                "sex" => 1,
                "language" => "zh_CN",
                "city" => "长沙",
                "province" => "湖南",
                "country" => "中国",
                "headimgurl" => "http://thirdwx.qlogo.cn/mmopen/kQnLI7Nh6s5ElZSo4HmEoSrWJRjkVjeUVdCWomMjwvAnaZg4ot2QWoXMKWxSQibFgZiaJSyGadfRJCu5XCnPCib6Efyf8gSCPyj/132",
                "subscribe_time" => 1512566332,
                "remark" =>"",
                "groupid" => 0,
                "tagid_list" => Array(),
                "subscribe_scene" => "ADD_SCENE_QR_CODE",
                "qr_scene" => 0,
            );
            session("user", $user);
        }

        //session("token", "figol");
        if (!session("token")){
            if(I("get.wechat_id")){ // 技术人员测试专用
                session("token", I("get.wechat_id"));
            }else{
                //使用jsapi接口，重新获取token
                vendor('WxPayPubHelper.WxPayPubHelper');
                $jsApi = new \wxPay\WxPayPubHelper\JsApi_pub();

                //=========步骤1：网页授权获取用户openid============
                //通过code获得openid
                if (!isset($_GET['code'])) {
                    //触发微信返回code码
                    $refer = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                    $url = $jsApi->createOauthUrlForCode($refer);
                    //$url = $jsApi->createOauthUrlForCode(\wxPay\WxPayPubHelper\WxPayConf_pub::JS_API_CALL_URL);
                    Header("Location: $url");
                } else {
                    //获取code码，以获取openid
                    $code = $_GET['code'];
                    $jsApi->setCode($code);
                    $openid = $jsApi->getOpenId();
                    if(array_key_exists("errcode", $openid)){
                        $refer = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                        header("Content-type: text/html; charset=utf-8");
                        echo '<h1>授权失败1:'.$openid['errmsg'].'，请<a href="'.$refer.'">点击此处</a>重新认证</h1>';
                        exit;
                    }
                    $user = $jsApi->getUserInfo($openid['openid']);
                    if(array_key_exists("errcode", $user)){
                        header("Content-type: text/html; charset=utf-8");
                        echo '<h1>授权失败2:'.$user['errmsg'].'，请<a href="'.$_SERVER['REQUEST_URI'].'">点击此处</a>重新认证</h1>';
                        exit;
                    }
                    session("token", $openid);
                    session("user", $user);
                    session("unionid", session("user")['unionid']);
                    session("openid", session("user")['openid']);
                    //print_r(session("user"));
                }
            }
        }
        $where = array("openid"=>session("openid"));
        $member = M("reziti_master")->where($where)->find();
        session("member", $member);
        $this->assign("member", $member);
        $this->assign("user", session("user"));
    }

}